package com.ruoyi.common.utils;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

public class SortMap {


    /**
     * 让 Map按key进行排序
     */
    public static Map<String, Object> sortMapByKey(Map<String, Object> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }
        Map<String, Object> sortMap = new TreeMap<String, Object>(new MapKeyComparator());
        sortMap.putAll(map);
        return sortMap;
    }

    //实现一个比较器类
    static class MapKeyComparator implements Comparator<String> {

        @Override
        public int compare(String s1, String s2) {
            return s1.compareTo(s2);  //从小到大排序
        }
    }

}
